//
//  UFOYZXTypeViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOYZXTypeViewController.h"
#import "Masonry.h"
#import "UFOYZXTypeTableView.h"
#import "LQCAFNetTool.h"
#import "UFOTypeVideoModel.h"
#import "UFOPlayerViewController.h"
@interface UFOYZXTypeViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *typeArr;
@property (nonatomic, assign) NSInteger number;
@end

@implementation UFOYZXTypeViewController
- (void)dealloc
{
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [_tableV release];
    [_typeArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.number = 1;
    
    self.typeArr = [NSMutableArray array];
    
    self.navigationItem.title = self.nameL;
    
    [self createTableView];
    
    [self getData];
    
    //下拉刷新
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getData];
        
        [self.tableV reloadData];
        
        [self.tableV.mj_header  endRefreshing];
    }];
    //上拉加载
    self.tableV.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        self.number ++;
        [self getData];
        [self.tableV.mj_footer endRefreshing];
    }];
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableV];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.rowHeight = 100;
}

- (void)getData
{
    
    NSString *url =  [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=l&p=%ld&tag=%@",self.number,self.tagL];
    NSLog(@"%ld", self.number);
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *tempDic in arr) {
            UFOTypeVideoModel *model = [[UFOTypeVideoModel alloc] initWithDic:tempDic];
            [self.typeArr addObject:model];

        }
        
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UFOPlayerViewController *playVC = [[UFOPlayerViewController alloc] init];
    UFOTypeVideoModel *model = [self.typeArr objectAtIndex:indexPath.row];
    playVC.videoId = model.vid;
    playVC.videoName = model.title;
    NSLog(@"%@", model.title);
    [self presentViewController:playVC animated:YES completion:^{
        
        
    }];
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您现在使用的是运营商网络,继续观看可能产生超额流量费" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"继续观看" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        
//        
//    }];
//    [alert addAction:yesAction];
//    [self presentViewController:alert animated:YES completion:^{
//        
//    }];
//    
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消观看" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        
//    }];
//    [alert addAction:cancelAction];
//    [self presentViewController:alert animated:YES completion:^{
//        
//    }];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.typeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellStr = @"cell";
    UFOYZXTypeTableView *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[UFOYZXTypeTableView alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    UFOTypeVideoModel *model = [self.typeArr objectAtIndex:indexPath.row];
    [cell.typeImageV sd_setImageWithURL:[NSURL URLWithString:model.cover_url]];
    cell.titleL.text = model.title;
    cell.introduceL.text = model.udb;
    cell.timeL.text = [model.upload_time substringWithRange:NSMakeRange(0, 11)];
    
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
